package com.yuecai.modules.yc_receiver;

import com.yuecai.modules.yc_receiver_info.domain.YcReceiverInfo;
import com.yuecai.modules.yc_receiver_info.service.YcReceiverInfoService;
import com.yuecai.modules.yc_receiver_info.service.dto.YcReceiverInfoDto;
import com.yuecai.modules.yc_receiver_info.service.dto.YcReceiverInfoQueryCriteria;
import com.yuecai.modules.yc_receiver_info.service.mapstruct.YcReceiverInfoMapper;
import com.yuecai.utils.SecurityUtils;
import com.yuecai.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* @author dm
* @date 2020-05-20
*/
@Slf4j
@RestController
@RequestMapping("/api/ycReceiver")
public class YcReceiverInfoController {

    private final YcReceiverInfoService ycReceiverInfoService;
    private final YcReceiverInfoMapper ycReceiverInfoMapper;

    public YcReceiverInfoController(YcReceiverInfoService ycReceiverInfoService, YcReceiverInfoMapper ycReceiverInfoMapper) {
        this.ycReceiverInfoService = ycReceiverInfoService;
        this.ycReceiverInfoMapper = ycReceiverInfoMapper;
    }


    /**
     * 查询收货人
     * @param criteria
     * @param pageable
     * @return
     */
    @GetMapping
    public ResponseEntity<Object> findReceiverInfos(YcReceiverInfoQueryCriteria criteria, Pageable pageable){
        String currentUsername = SecurityUtils.getCurrentUsername();
        criteria.setUserId(Long.valueOf(currentUsername));

        return new ResponseEntity<>(ycReceiverInfoService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    /**
     * 新增
     * @param receiverInfo
     * @return
     */
    @PostMapping
    public ResponseEntity<Object> create(@RequestBody YcReceiverInfo receiverInfo) throws Exception {
        String phoneNum = receiverInfo.getPhoneNum();
        if (isMobileNO(phoneNum)){
            throw new Exception("手机号不符合规范！");
        }
        YcReceiverInfoDto ycReceiverInfoDto = ycReceiverInfoService.create(receiverInfo);
        return new ResponseEntity<>(ycReceiverInfoDto,HttpStatus.OK);
    }

    @PostMapping("/edit")
    public ResponseEntity<Object> edit(@RequestBody YcReceiverInfo receiverInfo) throws Exception {
        String phoneNum = receiverInfo.getPhoneNum();
        if (StringUtils.isNotBlank(phoneNum)&&isMobileNO(phoneNum)){
            throw new Exception("手机号不符合规范！");
        }
        try {
            if (receiverInfo.getId()==null){
                throw new Exception("id，不能为空");
            }
            YcReceiverInfoDto byId = ycReceiverInfoService.findById(receiverInfo.getId());
            byId.setDefaultSign(receiverInfo.getDefaultSign());
            ycReceiverInfoService.update(ycReceiverInfoMapper.toEntity(byId));
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<>("修改成功！",HttpStatus.OK);
    }

    /**
     * 修改收货人
     * @param receiverInfo
     * @return
     */
    @PutMapping
    public ResponseEntity<Object> update(@Validated @RequestBody YcReceiverInfo receiverInfo) throws Exception {
        String phoneNum = receiverInfo.getPhoneNum();
        if (isMobileNO(phoneNum)){
            throw new Exception("手机号不符合规范！");
        }
        try {
            ycReceiverInfoService.update(receiverInfo);
        } catch (Exception e) {
            throw e;
        }
        return new ResponseEntity<>("修改成功！",HttpStatus.OK);
    }

    public static boolean isMobileNO(String mobiles){
        Pattern p = Pattern.compile("^((13[0-9])|(14[0|5|6|7|9])|(15[0-3])|(15[5-9])|(16[6|7])|(17[2|3|5|6|7|8])|(18[0-9])|(19[1|8|9]))\\d{8}$");
        Matcher m = p.matcher(mobiles);
        return !m.matches();
    }



    /**
     * 删除收货人
     * @param ids
     * @return
     */
    @DeleteMapping
    public ResponseEntity<Object> delete(@RequestBody Long[] ids) throws Exception {
        ycReceiverInfoService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}